package zozopo.warehouse;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedTB;
import zozopo.database.BusiDatabaseManager;
import zozopo.database.DatabaseManager;
import zozopo.flow.comm.Flow;
import zozopo.flow.comm.FlowCode;
import zozopo.user.operations.UserOS;
import zozopo.user.operations.UserTB;
import zozopo.warehouse.operations.ChangewarehouserecordOS;
import zozopo.warehouse.operations.ChangewarehouserecordTB;
import zozopo.warehouse.operations.PurchaseOS;
import zozopo.warehouse.operations.ReceiptOS;
import zozopo.warehouse.operations.ReceiptTB;
import zozopo.warehouse.operations.ReceiptlistOS;
import zozopo.warehouse.operations.ReceiptlistTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class AddChangeWarehouseAction extends ActionSupport implements Action,SessionAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String limitedCode ="WAREHOUSE.CHANGEWAREHOUSE.ADD";
	
	//传入参数
	private int goodsid_changewarehouserecord;
	private String goodunit_changewarehouserecord;
	private String goodsname_changewarehouserecord;
	
	private int warehouseid_changewarehouserecord;
	private String warehousename_changewarehouserecord;
	
	private int targetwarehouseid_changewarehouserecord;
	private String targetwarehousename_changewarehouserecord;
	
	private int number_changewarehouserecord;
	 
	private String remark_changewarehouserecord;

	

	
	//传出参数
	
	private ChangewarehouserecordTB changewarehouserecord;
	private String operationResult;
	private boolean sqlError = false;
	
	private Map<Object, Object> session;
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	
	
	public String execute(){
		
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		
		
		DatabaseManager databaseManager = new BusiDatabaseManager();
		Connection conn = databaseManager.getConnection();
		
		ChangewarehouserecordOS changewarehouserecordOS = new  ChangewarehouserecordOS(conn);
		PurchaseOS purchaseOS = new PurchaseOS(conn);
		changewarehouserecord = new ChangewarehouserecordTB();
		changewarehouserecord.setGoodsid_changewarehouserecord(goodsid_changewarehouserecord);
		changewarehouserecord.setGoodsname_changewarehouserecord(goodsname_changewarehouserecord);
		changewarehouserecord.setNumber_changewarehouserecord(number_changewarehouserecord);
		changewarehouserecord.setRemark_changewarehouserecord(remark_changewarehouserecord);
		
		changewarehouserecord.setTargetwarehouseid_changewarehouserecord(targetwarehouseid_changewarehouserecord);
		changewarehouserecord.setTargetwarehousename_changewarehouserecord(targetwarehousename_changewarehouserecord);
		
		changewarehouserecord.setUseremail_changewarehouserecord(operator.getEmail_operator());
		changewarehouserecord.setUsername_changewarehouserecord(operator.getName_operator());
		
		changewarehouserecord.setWarehouseid_changewarehouserecord(warehouseid_changewarehouserecord);
		changewarehouserecord.setWarehousename_changewarehouserecord(warehousename_changewarehouserecord);
		try {
			conn.setAutoCommit(false);

			
			if(!changewarehouserecordOS.insert(changewarehouserecord)){
				throw new Exception();
			}
			
			
			int warehouseid = changewarehouserecord.getWarehouseid_changewarehouserecord();
			int goodsid = changewarehouserecord.getGoodsid_changewarehouserecord();
			int number = changewarehouserecord.getNumber_changewarehouserecord();
			int targetwarehouseid = changewarehouserecord.getTargetwarehouseid_changewarehouserecord();
			
			int goodsnumber = purchaseOS.sumGoodsNumberById(goodsid,warehouseid);
			
			if(goodsnumber == 0){ //如果没有查到相关数量或库存数量为0
				throw new Exception();
			}
			
			if(number > goodsnumber){  //如果输出大于库存数量
				throw new Exception();
			}

			if(!purchaseOS.changeWarehouse(goodsid, number, warehouseid, targetwarehouseid)){ //递归减掉进仓的数量
				throw new Exception();
			}
			
			conn.commit();
			conn.setAutoCommit(true);
			
			changewarehouserecord = changewarehouserecordOS.get(changewarehouserecordOS.getMaxIdByEmail(operator.getEmail_operator()));
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			operationResult = "插入失败";
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}catch (Exception e) 
		{
			operationResult = "插入失败";
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			databaseManager.releaseConnection(conn);
		}
		
		
	
		
		return SUCCESS;//*/
		
	}
	
	
	
	public void validate() {
		
		
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operatorlimitedMap.get(limitedCode) == null){
			addFieldError("errormessage","用户没有更改仓库权限.");
			return;
		}
		
		if(warehouseid_changewarehouserecord == targetwarehouseid_changewarehouserecord){
			addFieldError("errormessage", "转换的仓库不能相同!");
			return;
		}
	}


	public int getGoodsid_changewarehouserecord() {
		return goodsid_changewarehouserecord;
	}


	public void setGoodsid_changewarehouserecord(int goodsid_changewarehouserecord) {
		this.goodsid_changewarehouserecord = goodsid_changewarehouserecord;
	}


	public String getGoodunit_changewarehouserecord() {
		return goodunit_changewarehouserecord;
	}


	public void setGoodunit_changewarehouserecord(
			String goodunit_changewarehouserecord) {
		this.goodunit_changewarehouserecord = goodunit_changewarehouserecord;
	}


	public String getGoodsname_changewarehouserecord() {
		return goodsname_changewarehouserecord;
	}


	public void setGoodsname_changewarehouserecord(
			String goodsname_changewarehouserecord) {
		this.goodsname_changewarehouserecord = goodsname_changewarehouserecord;
	}


	public int getWarehouseid_changewarehouserecord() {
		return warehouseid_changewarehouserecord;
	}


	public void setWarehouseid_changewarehouserecord(
			int warehouseid_changewarehouserecord) {
		this.warehouseid_changewarehouserecord = warehouseid_changewarehouserecord;
	}


	public String getWarehousename_changewarehouserecord() {
		return warehousename_changewarehouserecord;
	}


	public void setWarehousename_changewarehouserecord(
			String warehousename_changewarehouserecord) {
		this.warehousename_changewarehouserecord = warehousename_changewarehouserecord;
	}


	public int getTargetwarehouseid_changewarehouserecord() {
		return targetwarehouseid_changewarehouserecord;
	}


	public void setTargetwarehouseid_changewarehouserecord(
			int targetwarehouseid_changewarehouserecord) {
		this.targetwarehouseid_changewarehouserecord = targetwarehouseid_changewarehouserecord;
	}


	public String getTargetwarehousename_changewarehouserecord() {
		return targetwarehousename_changewarehouserecord;
	}


	public void setTargetwarehousename_changewarehouserecord(
			String targetwarehousename_changewarehouserecord) {
		this.targetwarehousename_changewarehouserecord = targetwarehousename_changewarehouserecord;
	}


	public int getNumber_changewarehouserecord() {
		return number_changewarehouserecord;
	}


	public void setNumber_changewarehouserecord(int number_changewarehouserecord) {
		this.number_changewarehouserecord = number_changewarehouserecord;
	}


	public String getRemark_changewarehouserecord() {
		return remark_changewarehouserecord;
	}


	public void setRemark_changewarehouserecord(String remark_changewarehouserecord) {
		this.remark_changewarehouserecord = remark_changewarehouserecord;
	}





	public String getOperationResult() {
		return operationResult;
	}


	public void setOperationResult(String operationResult) {
		this.operationResult = operationResult;
	}


	public ChangewarehouserecordTB getChangewarehouserecord() {
		return changewarehouserecord;
	}


	public void setChangewarehouserecord(
			ChangewarehouserecordTB changewarehouserecord) {
		this.changewarehouserecord = changewarehouserecord;
	}


	public static String getLimitedcode() {
		return limitedCode;
	}






}
